import { getQiniuTokenV2 } from "@/api"
import qiniu from "@/utils/qiniuUpload"
import storage from "@/utils/storage"
let env = 'online'
export function upload(type = 'daily') {
  return new Promise((resolve, reject) => {
    uni.chooseImage({
      count: 1,  //图片可选择数量
      sizeType: ['original', 'compressed'],  //original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album'],   //album 从相册选图，camera 使用相机，默认二者都有。
      success: async res => {
        let imgFiles = res.tempFilePaths   //图片的本地文件路径列表
        uni.showLoading({
          title: '上传中......',
          mask: true
        })
        let uptoken = ''

        const { code, data } = await getQiniuTokenV2()
        storage.setStorage('qiniuToken', data.image_token)
        uptoken = data.image_token
        qiniu.upload(
          imgFiles[0], //上传的图片
          (res) => {  //回调 success
            // this.form.source_url =  res.imageURL;
            resolve(res.imageURL)
          },
          (error) => { //回调 fail
            console.log('error: ' + error);
            reject()
          },
          { // 参数设置  地区代码 token domain 和直传的链接 注意七牛四个不同地域的链接不一样，我使用的是华南地区
            region: 'NCN',
            // ECN, SCN, NCN, NA, ASG，分别对应七牛的：华东，华南，华北，北美，新加坡 5 个区域
            uptoken: uptoken,   //调接口
            // uploadURL: 'https://content.astro90.com',
            key: data.image_file_path,
            domain: data.image_host,    //域名
          })
      },
      fail() {
        uni.hideLoading()
      }
    })
  })
}
export function uploadSync(imgFiles) {
  return new Promise(async resolve => {
    let uptoken = ''
    const { code, data } = await getQiniuTokenV2()
    storage.setStorage('qiniuToken', data.image_token)
    uptoken = data.image_token
    console.log(imgFiles)
    qiniu.upload(
      imgFiles, //上传的图片
      (res) => {  //回调 success
        // this.form.source_url =  res.imageURL;
        console.log(res, 'upload1111111111111111')
        resolve(res.imageURL)
      },
      (error) => { //回调 fail
        console.log('error: ' + error);
      },
      { // 参数设置  地区代码 token domain 和直传的链接 注意七牛四个不同地域的链接不一样，我使用的是华南地区
        region: 'NCN',
        // ECN, SCN, NCN, NA, ASG，分别对应七牛的：华东，华南，华北，北美，新加坡 5 个区域
        uptoken: uptoken,   //调接口
        // uploadURL: 'https://content.astro90.com',
        key: data.image_file_path,
        domain: data.image_host,    //域名
      })
  })
}
export function chooseImg() {
  return new Promise(resolve => {
    wx.showActionSheet({
      itemList: ['拍摄', '选择照片'],
      itemColor: '#333',
      success: res => {
        let sourceType = ['camera', 'album']
        if (res.tapIndex === 0) {
          sourceType = ['camera']
        } else {
          sourceType = ['album']
        }
        uni.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType,//camera 拍照 album 相册
          success: res => {
            resolve(res.tempFilePaths[0])
          },
          fail() {
            uni.showToast({
              title: "拍照或引用相册失败",
              duration: 2000
            })
          }
        })
      }
    })
  })
}
export function chooseVideo() {
  return new Promise(resolve => {
    wx.showActionSheet({
      itemList: ['拍摄', '选择视频'],
      itemColor: '#333',
      success: res => {
        let sourceType = ['camera', 'album']
        if (res.tapIndex === 0) {
          sourceType = ['camera']
        } else {
          sourceType = ['album']
        }
        uni.chooseVideo({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType,//camera 拍照 album 相册
          success: (res) => {
            console.log(res, 'chooseVideo')
            resolve(res)
          },
          fail() {
            uni.showToast({
              title: "拍照或引用相册失败",
              duration: 2000
            })
          }
        })
      }
    })
  })
}
